#include<iostream>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<utility>
#include<list>
#include<string>
#include<vector>
using namespace std;
#define maxn 200010
#define maxm 1000010
#define ll long long
inline int read()
{
    int f = 1, res = 0; char ch = getchar();
    while( ch < '0' || ch > '9' )
    {
        if( ch == '-' ) f = -1;
        ch = getchar();
    }
    while( '0' <= ch && ch <= '9' )
    {
        res = ( res << 1 ) + ( res << 3 ) + ( ch - '0' );
        ch = getchar();
    }
    return res * f;
}
inline void write( int x )
{
    if( x < 0 ) putchar( '-' ), x = -x;
    if( x > 9 ) write( x / 10 );
    putchar( x % 10 + '0' );
}
int cnt[ maxm ];
int g[ maxn ];
ll f[ maxn ][ 2 ];
int n, t, a[ maxn ];
ll ans = 0;
void solve()
{
    memset( cnt, -1, sizeof( cnt ) );
    memset( f, 0, sizeof( f ) );
    memset( g, -1, sizeof( g ) );
    n = read(); ans = 0;
    for( int i = 1; i <= n; i++ )
    {
        a[ i ] = read();
        if( cnt[ a[ i ] ] != -1 )
            g[ i ] = cnt[ a[ i ] ];
        else cnt[ a[ i ] ] = i;
    }
    for( int i = 1; i <= n; i++ )
    {
        if( g[ i ] != -1 )
        {
            f[ i ][ 0 ] = max( f[ i ][ 0 ], f[ g[ i ] ][ 0 ] + f[ i - 1 ][ 1 ] - f[ g[ i ] ][ 1 ] + a[ i ] );
            f[ i ][ 1 ] = max( f[ i ][ 1 ], f[ g[ i ] ][ 1 ] + f[ i - 1 ][ 0 ] - f[ g[ i ] ][ 0 ] + a[ i ] );
        }
    }
    /*for( int i = 1; i <= n; i++ )
        write( f[ i ][ 0 ] ), putchar( ',' );
    for( int i = 1; i <= n; i++ )
        write( f[ i ][ 1 ] ), putchar( ',' );*/
    ans = max( f[ n ][ 0 ], f[ n ][ 1 ] );
    write( ans );
    puts( "" );
}
signed main()
{
    freopen( "color1.in", "r", stdin );
    //freopen( "color.out", "w", stdout );
    t = read();
    while( t-- )
        solve();
    return 0;
}
